<?php
/**
 * 
 * @author huangjian E-mail:870596179@qq.com
 * @version 创建时间：2016年10月21日 上午10:42:24 
 * 类说明
 */
class AttributeValuesAction extends BaseAction{
	
	/**
	 * @authority 浏览
	 */
	public function index(){
		$id = $_REQUEST['id'];
		$this->assign("id",$id);
		$this->display('index');
	}
	
	//列表
	public function getAllData(){
		$Model = M($this->getActionName());
		$map = $this->loadSeachCondition();
		$map['product_id'] = $_REQUEST['id'];
		$productInfo = M("Product")->field("name")->where(array('id'=>$map['product_id'],'create_userid'=>$this->uid))->find();

		$map['flag'] = 1;
		$total = $Model->where($map)->count();// 查询满足要求的总记录数
		$page = json_decode($this->pageParam($total));
		$data = $Model->where($map)->limit($page->firstRow,$page->listRows)->order($page->sort)->select();
		for($i=0;$i<count($data);$i++){
			$data[$i]['product_name'] = $productInfo['name'];
		}
		$vo = $this->toJosnForGird($data,$page);
		echo json_encode($vo);
	}
	
	/**
	 * 组装条件
	 * @return multitype:multitype:string
	 */
	public function loadSeachCondition(){
		$map = array();
		/*
		if(!empty($_GET['field'])){
			$map['field'] = array("like","%".$_GET['field']."%");
		}
		*/
		return $map;
	}
	
	// 新增
	public function add(){
		$id = $_REQUEST['id'];
		$AttributeModel = M("Attribute");
		$attribute = $AttributeModel->where("flag=1")->select();
		$this->assign("attribute",$attribute);
		$this->assign("product_id",$id);
		$this->display();
	}

	public function insert(){
		$Product = M("Product");
		$Model = M($this->getActionName());
		unset ( $_POST [$Model->getPk()] );
		if (false === $Model->create()) {
			$this->error($Model->getError());
		}
		$data = $Model->create();
		if(!empty($data['attr_id'])){
			$attribute = M("Attribute")->field("name")->where(array('id'=>$data['attr_id']))->find();
			$data['attr_name'] = $attribute['name'];
		}
		$map['flag'] = 1;
		$map['create_userid']=$this->uid;
		$max_seq = $Model->where($map)->order('seq desc')->getField('seq');
		$data['seq'] = $max_seq + 1;
		$data['create_userid']=$this->uid;
		//保存当前数据对象
		if ($result = $Model->add($data)) { //保存成功
			if(isset($data['product_id']) && $data['product_id']){
				$Product->where(array('id'=>$data['product_id']))->setField('is_attr',1);
			}
			$this->ajaxReturn(1,L('ADMIN_ADD_SUCCESS'),1);
		} else {
			$this->ajaxReturn(0,L('ADMIN_ADD_ERROR'),0);
		}
	}
	
	
	
	//修改
	public function edit() {
		$AttributeModel = M("Attribute");
		$attribute = $AttributeModel->where("flag=1")->select();
		$this->assign("attribute",$attribute);
		$Model = M($this->getActionName());
		$map['id'] = $_REQUEST['id'];
		$map['create_userid']=$this->uid;
		$vo = $Model->where($map)->find();
		$this->assign('vo',$vo);
		$this->display();
	}
	
	/**
	 * 更新
	 */
	public function update() {
		$Model = D($this->getActionName());
		if(false === $Model->create()) {
			$this->error($Model->getError());
		}
		// 更新数据
		if(false !== $Model->save()) {
			$this->ajaxReturn(1,L('ADMIN_EDIT_SUCCESS'),1);
		} else {
			$this->ajaxReturn(0,L('ADMIN_EDIT_ERROR'),0);
		}
	}
	
	/**
	 * 逻辑删除
	 */
	public function logicDel(){
		$Model = M($this->getActionName());
		if (!empty($Model)) {
			$pk = $Model->getPk();
			$id = $_REQUEST[$pk];
			if (isset($id)) {
				$condition = array($pk => array('in', explode(',', $id)));
				$condition['create_userid']=$this->uid;
				if (false !== $Model->where($condition)->setField('flag',2)) {
					$this->ajaxReturn(1,L('ADMIN_DELETE_SUCCESS'),1);
				} else {
					$this->ajaxReturn(0,L('ADMIN_DELETE_ERROR'),0);
				}
			} else {
				$this->ajaxReturn(0,L('ADMIN_DELETE_ERROR'),0);
			}
		}
	}
}